﻿using System;
using System.Windows;
using System.Text;

namespace WPFHostsWin32
{
    public partial class MainWindow : Window
    {
        private ControlHost listControl;

        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // Create control.
            this.listControl = new ControlHost(
                this.ControlContent.ActualHeight, 
                this.ControlContent.ActualWidth);

            // Add control to the Visual Tree and configure message filter.
            this.ControlContent.Child = this.listControl;
            this.listControl.MessageHook += MessageFilter;

            // Add dummy items to the list.
            for (int i = 0; i < 15; i++)
            {
                NativeMethods.SendMessage(
                    this.listControl.Hwnd,
                    NativeMethods.LB_ADDSTRING,
                    IntPtr.Zero,
                    string.Format("Item: {0}", i));
            }

            // Update count.
            int count = NativeMethods.SendMessage(this.listControl.Hwnd, NativeMethods.LB_GETCOUNT, IntPtr.Zero, IntPtr.Zero);
            this.ItemCount.Text = count.ToString();
        }

        private void AddItem(object sender, RoutedEventArgs e)
        {
            if (this.ItemText.Text.Length == 0)
            {
                return;
            }

            // Add new item to the list.
            NativeMethods.SendMessage(
                this.listControl.Hwnd,
                NativeMethods.LB_ADDSTRING,
                IntPtr.Zero,
                string.Format("{0}", this.ItemText.Text));

            // Update UI controls.
            int count = NativeMethods.SendMessage(this.listControl.Hwnd, NativeMethods.LB_GETCOUNT, IntPtr.Zero, IntPtr.Zero);
            this.ItemCount.Text = count.ToString();
            this.ItemText.Text = string.Empty;
        }

        private void DeleteItem(object sender, RoutedEventArgs e)
        {
            // Find the selected item
            int selectedIndex = NativeMethods.SendMessage(this.listControl.Hwnd, NativeMethods.LB_GETCURSEL, IntPtr.Zero, IntPtr.Zero);
            if (selectedIndex == -1)
            {
                return;
            }

            // Delete the item.
            NativeMethods.SendMessage(this.listControl.Hwnd, NativeMethods.LB_DELETESTRING, (IntPtr)selectedIndex, IntPtr.Zero);

            // Update count.
            int count = NativeMethods.SendMessage(this.listControl.Hwnd, NativeMethods.LB_GETCOUNT, IntPtr.Zero, IntPtr.Zero);
            this.ItemCount.Text = count.ToString();
        }

        IntPtr MessageFilter(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == NativeMethods.WM_COMMAND)
            {
                switch ((uint)wParam.ToInt32() >> 16 & 0xFFFF) // Switch on the HIWORD in the message.
                {
                    case NativeMethods.LBN_SELCHANGE:
                        // Extract the selected text from the ListBox.
                        StringBuilder itemtext = new StringBuilder();
                        int selectedIndex = NativeMethods.SendMessage(this.listControl.Hwnd, NativeMethods.LB_GETCURSEL, IntPtr.Zero, IntPtr.Zero);
                        NativeMethods.SendMessage(this.listControl.Hwnd, NativeMethods.LB_GETTEXT, selectedIndex, itemtext);
                        
                        // Update the UI.
                        this.SelectedText.Text = itemtext.ToString();
                        
                        handled = true;
                        break;
                }
            }

            return IntPtr.Zero;
        }
    }
}